//
//  DetailViewController.m
//  ProjectA
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/6.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"2222");
    [self reloadInputViews];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"1111");
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.title = self.Labelstr1;
    
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    if ([wifi currentReachabilityStatus] == NotReachable && [conn currentReachabilityStatus] == NotReachable) {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前网络不佳" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alterView show];
        
    }

    self.myArray = [NSMutableArray array];
    self.scrollView = [[DetailScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height )];
    NSURL *url = [NSURL URLWithString:self.LabelStr2];
    [self.scrollView.ImageView  sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_Phone@2x.png"]];
    self.detailArray = [NSMutableArray array];
    self.detailDic = [NSMutableDictionary dictionary];
    
    
    NSString *string = [self.LabelStr2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string1 = [NSString stringWithFormat:@"http://68.168.105.137/cookbook/master/master.php?rurl=%@&devid=20e94d365e82aa2f&country=CN&appname=com.riatech.cookbook&simcountry=cn&from=%@&lang=zh-CN", self.LabelStr3, string];
    
    [AFNDownLoader getDataDownLoad:string1 block:^(id myData) {
        NSMutableDictionary *BigDic = myData;
        NSMutableArray *array = [BigDic objectForKey:@"recipe"];
        
        
        NSArray *array1 = [array subarrayWithRange:NSMakeRange(0, array.count - 4)];
        
        NSString *string1 = [NSString string];
        for (NSMutableDictionary *dic in array1) {
            
            string1 = [string1 stringByAppendingString:[dic objectForKey:@"ingredient"]];
        }
        
        
        // 适配
        if (self.view.frame.size.width == 375) {
            NSLog(@"start   ");
        CGRect frame1 = self.scrollView.materialContextLabel.frame;
    
        self.scrollView.materialContextLabel.text = string1;
        frame1.size.height = [[self class] heightForText:string1];
        NSLog(@"newframe :%f",frame1.size.height);
        self.scrollView.materialContextLabel.frame = frame1;
        
        self.scrollView.directionLabel.frame = CGRectMake(frame1.origin.x, frame1.origin.y + frame1.size.height + 30, 50, 20);
        NSLog(@"8/8/8/%f %f", self.scrollView.materialContextLabel.frame.origin.x, self.scrollView.materialContextLabel.frame.origin.y);
         // self.scrollView.lableHeigh = frame1.size.height;
        NSLog(@"matreial :%f ",self.scrollView.materialContextLabel.frame.size.height);
        NSMutableDictionary *dic = [array objectAtIndex:array.count - 4];
        NSLog(@"dic %@", dic);

        
        
        
        CGRect frame = self.scrollView.directionLabelTextLabel.frame;
        frame.size.height = [[self class] heightForText:[dic objectForKey:@"direction"] ];
        self.scrollView.directionLabelTextLabel.frame = CGRectMake(self.scrollView.directionLabel.frame.origin.x , self.scrollView.directionLabel.frame.origin.y + self.scrollView.directionLabel.frame.size.height + 20, self.view.frame.size.width - 30, frame.size.height );
        self.scrollView.directionLabelTextLabel.text = [dic objectForKey:@"direction"];
        NSLog(@"direction%@", self.scrollView.directionLabelTextLabel.text);
        

        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, frame.size.height + frame1.size.height + 520);
        }
        if (self.view.frame.size.width == 320) {
            CGRect frame1 = self.scrollView.materialContextLabel.frame;
            
            self.scrollView.materialContextLabel.text = string1;
            frame1.size.height = [[self class] heightForText1:string1];
            NSLog(@"newframe :%f",frame1.size.height);
            self.scrollView.materialContextLabel.frame = frame1;
            
            self.scrollView.directionLabel.frame = CGRectMake(frame1.origin.x, frame1.origin.y + frame1.size.height + 30, 50, 20);
            NSLog(@"8/8/8/%f %f", self.scrollView.materialContextLabel.frame.origin.x, self.scrollView.materialContextLabel.frame.origin.y);
            // self.scrollView.lableHeigh = frame1.size.height;
            NSLog(@"matreial :%f ",self.scrollView.materialContextLabel.frame.size.height);
            NSMutableDictionary *dic = [array objectAtIndex:array.count - 4];
            NSLog(@"dic %@", dic);
            
            
            
            
            CGRect frame = self.scrollView.directionLabelTextLabel.frame;
            frame.size.height = [[self class] heightForText1:[dic objectForKey:@"direction"] ];
            self.scrollView.directionLabelTextLabel.frame = CGRectMake(self.scrollView.directionLabel.frame.origin.x , self.scrollView.directionLabel.frame.origin.y + self.scrollView.directionLabel.frame.size.height + 20, self.view.frame.size.width - 30, frame.size.height );
            self.scrollView.directionLabelTextLabel.text = [dic objectForKey:@"direction"];
            NSLog(@"direction%@", self.scrollView.directionLabelTextLabel.text);
            
            
            self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, frame.size.height + frame1.size.height + 520);
        }if (self.view.frame.size.width == 414) {
            CGRect frame1 = self.scrollView.materialContextLabel.frame;
            
            self.scrollView.materialContextLabel.text = string1;
            frame1.size.height = [[self class] heightForText2:string1];
            NSLog(@"newframe :%f",frame1.size.height);
            self.scrollView.materialContextLabel.frame = frame1;
            
            self.scrollView.directionLabel.frame = CGRectMake(frame1.origin.x, frame1.origin.y + frame1.size.height + 30, 50, 20);
            NSLog(@"8/8/8/%f %f", self.scrollView.materialContextLabel.frame.origin.x, self.scrollView.materialContextLabel.frame.origin.y);
            // self.scrollView.lableHeigh = frame1.size.height;
            NSLog(@"matreial :%f ",self.scrollView.materialContextLabel.frame.size.height);
            NSMutableDictionary *dic = [array objectAtIndex:array.count - 4];
            NSLog(@"dic %@", dic);
            
            
            
            
            CGRect frame = self.scrollView.directionLabelTextLabel.frame;
            frame.size.height = [[self class] heightForText:[dic objectForKey:@"direction"] ];
            self.scrollView.directionLabelTextLabel.frame = CGRectMake(self.scrollView.directionLabel.frame.origin.x , self.scrollView.directionLabel.frame.origin.y + self.scrollView.directionLabel.frame.size.height + 20, self.view.frame.size.width - 30, frame.size.height );
            self.scrollView.directionLabelTextLabel.text = [dic objectForKey:@"direction"];
            NSLog(@"direction%@", self.scrollView.directionLabelTextLabel.text);
            
            
            self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, frame.size.height + frame1.size.height + 520);
 
            
        }
        
        
        
        [self.view addSubview:self.scrollView];
        
        [self.HUD removeFromSuperview];
    
    }];
    
//    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.HUD.labelText = @"正在加载";
    [self.view addSubview:self.HUD];
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        [self showtime];
        
    } completionBlock:^{
        
        
    }];
    
    
    //self.tabBarController.tabBar.hidden = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(METHOD)];
    
    self.isOn = NO;
    UIImage *image = [UIImage imageNamed:@"star_faved@2x.png"];
   image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(method1)];
    
    
    // Do any additional setup after loading the view.
}

-(void)method1
{
    UIActionSheet *sheetAction = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"返回" destructiveButtonTitle:nil otherButtonTitles:@"收藏",@"分享", nil];
    sheetAction.frame = CGRectMake(30, 340, 375, 200);
    [sheetAction showInView:self.view];
    
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[NewDetailDataBase shareDetailDataBaseHandle] openDB];
    [[NewDetailDataBase shareDetailDataBaseHandle] creatTable];
    NewDetailModel *detail = [[NewDetailModel alloc] init];
    detail.urlStr = self.LabelStr3;
    detail.imagestr = self.LabelStr2;
    detail.title = self.Labelstr1;
     NSInteger temp = 0;
    if (buttonIndex == 0 ) {
        for (NewDetailModel *detail in [[NewDetailDataBase shareDetailDataBaseHandle] selectDetail]) {
            if ([detail.title isEqualToString:self.Labelstr1]) {
                temp++;
            }
        }
        if (temp != 0) {
            UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"注意" message:@"该项目已经被收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alterView show];
        }

        if (temp == 0) {
            [[NewDetailDataBase shareDetailDataBaseHandle] insertDeatail:detail];
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
        }
    }
    // 点击分享按钮 分享到新浪微博
    if (buttonIndex == 1) {
        
        [UMSocialSnsService presentSnsIconSheetView:self appKey:[NSString stringWithFormat:@"507fcab25270157b37000010"] shareText:[NSString stringWithFormat:@"%@的样子看起来很好吃,推荐你也来品尝(来自上得厅堂)", self.navigationItem.title] shareImage:self.scrollView.ImageView shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil] delegate:self];

    }
    
    self.isOn = !self.isOn;
}


-(void)METHOD
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)showtime
{
    sleep(10);
}

// iphone6 的自适应高度
+ (CGFloat)heightForText:(NSString *)text
{
    
    
    CGSize size = CGSizeMake(345, 3000);
      NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    NSLog(@"frame %f, %f",frame.size.width,frame.size.height);
    
    return frame.size.height ;
}

// iphone 5 5s 4 4s 的自适应高度
+ (CGFloat)heightForText1:(NSString *)text1
{
    
    
    CGSize size = CGSizeMake(290, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text1 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    NSLog(@"frame %f, %f",frame.size.width,frame.size.height);
    
    return frame.size.height ;
}

// iphone 6plus的自适应高度
+ (CGFloat)heightForText2:(NSString *)text2
{
    
    
    CGSize size = CGSizeMake(384, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text2 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    NSLog(@"frame %f, %f",frame.size.width,frame.size.height);
    
    return frame.size.height ;
}

+ (CGFloat)heightForText3:(NSString *)text3
{
    
    
    CGSize size = CGSizeMake(290, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName];
    
    CGRect frame = [text3 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    NSLog(@"frame %f, %f",frame.size.width,frame.size.height);
    
    return frame.size.height ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
